package com.example.mybatisplus.service;

import com.example.mybatisplus.mapper.UserMapper;
import com.example.mybatisplus.model.User;
import com.example.mybatisplus.util.MyBatisPlusUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.io.IOException;

@Slf4j
public class UserService {
    public User getUserById(int id) throws IOException {
//        try (SqlSession session = MyBatisPlusUtil.getSqlSessionFactory().openSession()) {
//            UserMapper userMapper = session.getMapper(UserMapper.class);
//            return userMapper.getUserById(id);
//        }
        SqlSessionFactory sqlSessionFactory = MyBatisPlusUtil.initSqlSessionFactory();
        try (SqlSession session = sqlSessionFactory.openSession()) {
            UserMapper userMapper = session.getMapper(UserMapper.class);
            User user = userMapper.selectById(id);
            log.info(user.toString());
            return user;
        }
    }

}